<template>
  <div class="app-container">
    <el-table
      v-loading="listLoading"
      :data="list"
      element-loading-text="Loading"
      border
      fit
      highlight-current-row
    >
      <el-table-column align="center" label="预约者姓名">
        <template slot-scope="scope">
          {{ scope.row.name }}
        </template>
      </el-table-column>
      <el-table-column label="性别"  align="center">
        <template slot-scope="scope">
          <span>{{ scope.row.gender.text }}</span>
        </template>
      </el-table-column>
      <el-table-column label="手机号"  align="center">
        <template slot-scope="scope">
          {{ scope.row.mobile }}
        </template>
      </el-table-column>
      <el-table-column label="时间"  align="center">
        <template slot-scope="scope">
          {{ changeTime(scope.row.createTime) }}
        </template>
      </el-table-column>
      <el-table-column class-name="status-col" label="操作"  align="center">
        <template slot-scope="scope">
          <span v-if="scope.row.handleTime">已处理</span>
          <el-button v-else type="success" size="small" plain @click="agree(scope.row.id,scope.$index)">处理</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div class="right" style="margin-top: 20px">
      <pagination @fetchData="fetchData" :total="total"/>
    </div>
  </div>
</template>

<script>
import { preordains,preordainHandle } from '@/api/building'
import pagination from '@/components/Pagination/index'
import { parseTime } from '@/utils/index'
import { getToken } from '../../utils/auth'

export default {
  components:{
    pagination
  },
  data() {
    return {
      total:0,
      list: null,
      listLoading: true
    }
  },
  created() {
    this.fetchData({limit:10,page:0})
  },
  methods: {
    agree(id,index){
      this.$confirm('确认已经处理？')
        .then(_ => {
          preordainHandle({ "targetId": id,
            "token": getToken()}).then(res=>{
              if(res.state==200){
                this.list[index].handleTime=1
                this.$message.success('处理成功')
              }else{
                this.$message.error('处理失败')
              }
          })
        })
        .catch(_ => {});
    },
    changeTime(time){
      return parseTime(time)
    },
    fetchData(data) {
      this.listLoading = true
      preordains({limit:data.limit,page:data.page}).then(res => {
        this.total=res.total
        this.list = res.data
      }).finally(()=>{
        this.listLoading = false
      })
    },
    handleSizeChange(val) {
      this.limit=val
      this.fetchData()
      console.log(`每页 ${val} 条`)
    },
    handleCurrentChange(val) {
      this.page=val-1
      this.fetchData()

      console.log(`当前页: ${val}`)
    }
  }
}
</script>
<style lang="scss" scoped>

</style>
